package com.hxkcloud.patient.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.utils.DataUtils;
import com.hxkcloud.patient.utils.ScreenUtils;
import com.hxkcloud.patient.views.SpringProgressView;

import org.json.JSONException;
import org.json.JSONObject;

public class ScoreInfoActivity extends BaseActivity
{
    private SpringProgressView progressScore1;
    private ImageView iv_point1,tv_location1;
    private TextView tv_score;


    private SpringProgressView progressScore2;
    private ImageView iv_point2,tv_location2;
    private TextView tv_state;

    private SpringProgressView progressScore3;
    private ImageView iv_point3,tv_location3;
    private TextView tv_percent;

    private TextView tv_instr;
    private TextView tv_date;
    private ImageView iv_back;

    private LinearLayout liner_status1,liner_status2,liner_status3;


    private String content;
    private String name,percent,instr,score,date;

    private int maxValue1 = 9;
    private int currentValue1 = 0;

    private int maxValue2 = 2;
    private int currentValue2 = 0;

    private int maxValue3 = 20;
    private int currentValue3 = 0;



    public static void jumpScoreInfoActivity(Activity activity,String content,String data)
    {
        Intent intent = new Intent(activity,ScoreInfoActivity.class);
        intent.putExtra("content",content);
        intent.putExtra("data",data);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score_info);
        initView();
        initListener();
        initData();
    }
    @Override
    protected void updateByHandler(Message msg)
    {


    }

    @Override
    protected void initView()
    {
        progressScore1 = (SpringProgressView) findViewById(R.id.progressScore1);
        iv_point1 = (ImageView) findViewById(R.id.iv_point1);
        tv_location1 = (ImageView) findViewById(R.id.tv_location1);
        tv_score = (TextView) findViewById(R.id.tv_score);

        progressScore2 = (SpringProgressView) findViewById(R.id.progressScore2);
        iv_point2 = (ImageView) findViewById(R.id.iv_point2);
        tv_location2 = (ImageView) findViewById(R.id.tv_location2);
        tv_state = (TextView) findViewById(R.id.tv_state);

        progressScore3 = (SpringProgressView) findViewById(R.id.progressScore3);
        iv_point3 = (ImageView) findViewById(R.id.iv_point3);
        tv_location3 = (ImageView) findViewById(R.id.tv_location3);
        tv_percent = (TextView) findViewById(R.id.tv_percent);

        tv_instr = (TextView) findViewById(R.id.tv_instr);
        tv_date = (TextView) findViewById(R.id.tv_date);
        iv_back = (ImageView) findViewById(R.id.iv_back);

        liner_status1 = (LinearLayout) findViewById(R.id.liner_status1);
        liner_status2 = (LinearLayout) findViewById(R.id.liner_status2);
        liner_status3 = (LinearLayout) findViewById(R.id.liner_status3);


    }
    @Override
    protected void initListener()
    {
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    @Override
    protected void initData()
    {
        content = getIntent().getStringExtra("content");
        date = getIntent().getStringExtra("data");
        try
        {
            Log.i("---DD->","date:"+date);
            JSONObject data = new JSONObject(content);
            name = data.getString("RiskOfStrokeAgain");
            percent = data.getString("IncidenceOfStroke");
            instr = data.getString("AssessmentReport");
            score = data.getString("Score");
        } catch (JSONException e)
        {
            e.printStackTrace();
        }

        currentValue1 = Integer.valueOf(score);
        progressScore1.setMaxCount(maxValue1);
        progressScore1.setCurrentCount(currentValue1);
        tv_score.setText(score+"分");
        if(currentValue1 < 4 )
        {
            liner_status1.setBackground(getResources().getDrawable(R.drawable.score_bg1));
        }else if(currentValue1 < 7 && currentValue1 > 3)
        {
            liner_status1.setBackground(getResources().getDrawable(R.drawable.score_bg2));
        }else if(currentValue1 > 6)
        {
            liner_status1.setBackground(getResources().getDrawable(R.drawable.score_bg3));
        }

        if("低危".equals(name))
        {
            currentValue2 = 0;
            liner_status2.setBackground(getResources().getDrawable(R.drawable.score_bg1));
        }else if("中危".equals(name))
        {
            currentValue2 = 1;
            liner_status2.setBackground(getResources().getDrawable(R.drawable.score_bg2));
        }else
        {
            currentValue2 = 2;
            liner_status2.setBackground(getResources().getDrawable(R.drawable.score_bg3));
        }
        progressScore2.setMaxCount(maxValue2);
        progressScore2.setCurrentCount(currentValue2);
        tv_state.setText(name);


        if(percent.contains("%"))
        {
            percent = percent.replace("%","");
        }
        currentValue3 = Double.valueOf(percent).intValue();

        progressScore3.setMaxCount(maxValue3);
        progressScore3.setCurrentCount(currentValue3);

        liner_status3.setBackground(getResources().getDrawable(R.drawable.score_bg1));

        tv_percent.setText(percent+"%");

        tv_instr.setText("\u3000\u3000\u3000"+instr);
        tv_date.setText(DataUtils.getStrTime_ymd_hms(date));


    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus)
        {
            setUiPosition(progressScore1,iv_point1,tv_location1,maxValue1,currentValue1);
            setUiPosition(progressScore2,iv_point2,tv_location2,maxValue2,currentValue2);
            setUiPosition(progressScore3,iv_point3,tv_location3,maxValue3,currentValue3);
        }
    }


    public void setUiPosition(SpringProgressView progressView,ImageView point,ImageView pointLocation,int MaxValue,double CurrentValue)
    {
        int width = progressView.getWidth();
        Log.i("--width-->","width:"+width);
        Log.i("--width-->","MaxValue:"+MaxValue);
        Log.i("--width-->","CurrentValue:"+CurrentValue);
        Log.i("--width-->","==========================================");



        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ScreenUtils.dipToPx(ScoreInfoActivity.this,10),ScreenUtils.dipToPx(ScoreInfoActivity.this,10));
        params.addRule(RelativeLayout.CENTER_VERTICAL);
        int margin1 = (int) (width*CurrentValue/MaxValue - ScreenUtils.dipToPx(ScoreInfoActivity.this,15));
        if(margin1 < 0)
        {
            margin1 = 0;
        }
        params.setMargins(margin1,0,0,0);
        point.setLayoutParams(params);


        RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(ScreenUtils.dipToPx(ScoreInfoActivity.this,40), ViewGroup.LayoutParams.WRAP_CONTENT);
        params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        int margin2 = (int) (width*CurrentValue/MaxValue + ScreenUtils.dipToPx(ScoreInfoActivity.this,10));
        Log.i("--margin2-->","margin2:"+margin2);
        if(margin2 < ScreenUtils.dipToPx(ScoreInfoActivity.this,25))
        {
            margin2 = ScreenUtils.dipToPx(ScoreInfoActivity.this,25);
        }
        params1.setMargins(margin2,0,0,ScreenUtils.dipToPx(ScoreInfoActivity.this,14));
        pointLocation.setLayoutParams(params1);
    }

}
